﻿//1.全局变量
 var floorMax=0;//最大楼层数
//2.初始化界面展示方法

//管理区域
function fn_areaGrid() {
    $('#areaGrid').datagrid({
        rownumbers: true,
        singleSelect: true,
        fitColumns: true,
        width: 1700,
        url:'../Controller.aspx?controller=RoomController&method=GetAreaList',
        loadMsg:'正在加载数据',
        toolbar: [{
            id: 'btnreload-detail',
            text: '刷新',
            iconCls: 'icon-reload',
            handler: function() {
                 $('#areaGrid').datagrid('reload');
            }
        },{
            id: 'btnadd-detail',
            text: '添加',
            iconCls: 'icon-add',
            handler: function() {
                $('#dialog-area').dialog({ title: '添加管理区域' });
                $("#areaCode").removeAttr("readonly");
                $("#areaform").form('clear');
                $('#formareatype').val('add');
            }
        },{
            id: 'btnedit-detail',
            text: '修改',
            iconCls: 'icon-edit',
            handler: function() { 
                    var selected = $('#areaGrid').datagrid('getSelected');
                    if(selected)
                    {
                      $('#dialog-area').dialog({ title: '修改管理区域' });
                      $("#areaform").form("load", selected);
                      $("#areaCode").attr("readonly", "true");
                       $('#formareatype').val('alter');
                      $('#dialog-area').dialog('open');
                    }                 
            }
        }, {
            id: 'btndel-detail',
            text: '删除',
            iconCls: 'icon-no',
            handler: function() {
                   var SelectedNode = $('#areaGrid').datagrid('getSelected');
                    if(SelectedNode==null)
                    {
                      alert('请您选择要删除的记录！');
                      return;
                    }
                    if (!confirm("确定要删除吗？"))
                        return false;
                    var data = {areaCode: SelectedNode.AreaCode};
                    requestAjax("../Controller.aspx?controller=RoomController&method=DelArea",data, function(){
                        $('#areaGrid').datagrid('reload');
                         $("#areaform").form('clear');
                    });
           }
     }],
      onClickRow: function(rowindex,rowData) {
            grid_building_load(rowData.AreaCode);
        }       
        });
    }
   
 function grid_building_load(value){
    $('#buildingGrid').datagrid({url:'../Controller.aspx?controller=RoomController&method=GetBuildingList&AreaCode='+(typeof value=='undefined'?'':value)});
}
//楼宇   
function fn_buildingGrid() {
    $('#buildingGrid').datagrid({
        width: 1490,
        fitColumns: true,
        rownumbers: true,
        singleSelect: true,
        toolbar: [{
            id: 'btnreload-detail',
            text: '全部显示',
            iconCls: 'icon-reload',
            handler: function() {
               grid_building_load();
            }
        }, {
            id: 'btnadd-detail',
            text: '添加',
            iconCls: 'icon-add',
            handler: function() {
              $('#areaid').combobox({url:'../Controller.aspx?controller=RoomController&method=GetCombAreaList'});
                var SelectedNode = $('#areaGrid').datagrid('getSelected');
                if (SelectedNode) {
                    $('#dialog-building').dialog({ title: '添加楼宇' });
                    $("#buildingCode").removeAttr("readonly");
                    $("#buildingform").form('clear');
                     $('#areaid').combobox('setValue',SelectedNode.AreaCode);
                    //$("#PAreaCode").val(SelectedNode.AreaCode);
                     $('#formbuildtype').val('add');
                }
            }
        }, {
            id: 'btnedit-detail',
            text: '修改',
            iconCls: 'icon-edit',
            handler: function() {
              $('#areaid').combobox({url:'../Controller.aspx?controller=RoomController&method=GetCombAreaList'});
                var selected = $('#buildingGrid').datagrid('getSelected');
                if(selected)
                {
                  $('#dialog-building').dialog({ title: '修改楼宇信息' });
                  $("#buildingform").form("load", selected);
                  $("#buildingCode").attr("readonly", "true");
                   $('#formbuildtype').val('alter');
                  $('#dialog-building').dialog('open');
                }
            }
        }, {
            id: 'btndel-detail',
            text: '删除',
            iconCls: 'icon-no',
            handler: function() {
                 var SelectedNode = $('#buildingGrid').datagrid('getSelected');
                    if(SelectedNode==null)
                    {
                      alert('请您选择要删除的记录！');
                      return;
                    }
                 if (SelectedNode) {
                    if (!confirm("确定要删除吗？"))
                        return false;
                    var data = { buildingCode: SelectedNode.BuildingCode };
                    requestAjax("../Controller.aspx?controller=RoomController&method=DelBuilding", data, function(retobject) {
                        $('#buildingGrid').datagrid('reload');
                    });
                } 
            }
     }]
        });
    }
     
             
 //房间
function fn_roomGrid() {
    $('#roomGrid').datagrid({
        fit: true,
        fitColumns: true,
        url:'../Controller.aspx?controller=RoomController&method=GetRoomList',
        rownumbers: true,
        singleSelect: true,
        pagination: true,
        pageSize:'20',
        toolbar: [{
            id: 'btnsearch-detail',
            text: '查找',
            iconCls: 'icon-search',
            handler: function() {
                $('#dialog-search').dialog({ title: '查找房间' });
                 $("#searchform").form('clear');
                $('#dialog-search').dialog('open');               
            }
        },{
            id: 'btnreload-detail',
            text: '刷新',
            iconCls: 'icon-reload',
            handler: function() {
               $('#roomGrid').datagrid('reload');
            }
        },{
            id: 'btnadd-detail',
            text: '添加',
            iconCls: 'icon-add',
            handler: function() {
                $('#dialog-room').dialog({ title: '添加房间' });
                $("#pRoomNumber").removeAttr("readonly");
                $("#roomform").form('clear');
                $("#pRoomNumber").focus();
                var arecode=$('#pArea').combobox('getData')[0].AreaCode; 
                 $('#pArea').combobox('setValue',arecode);
                $('#pBuilding').combobox('options').url = '../Controller.aspx?controller=RoomController&method=GetBuildingFieldList&AreaCode=' + arecode;
                 $('#pBuilding').combobox('reload');
                $('#dialog-room').dialog('open');               
            }
        },{
            id: 'btnadd-detail',
            text: '批量添加',
            iconCls: 'icon-add',
            handler: function() {
                $('#dialog-batchadd').dialog({ title: '批量添加房间' });
                $('#dialog-batchadd').dialog('open');               
            }
        }, {
            id: 'btnedit-detail',
            text: '修改',
            iconCls: 'icon-edit',
            handler: function() {
                 var selected = $('#roomGrid').datagrid('getSelected');
                if(selected!=null)
                {
                  $('#dialog-room').dialog({ title: '修改房间信息' });
                  $("#roomform").form("load", selected);
                  $("#pRoomNumber").attr("readonly", "true");
                  $('#dialog-room').dialog('open');
                }
            }
        }, {
            id: 'btndel-detail',
            text: '删除',
            iconCls: 'icon-no',
            handler: function() {
                 var SelectedNode = $('#roomGrid').datagrid('getSelected');
                 if (SelectedNode!=null) {
                    if (!confirm("确定要删除吗？"))
                        return false;
                    var data = { RoomId: SelectedNode.RoomId };
                    requestAjax("../Controller.aspx?controller=RoomController&method=DelRoom", data, function(retobject) {
                        $('#roomGrid').datagrid('reload');
                    });
                } 
            }
     }]
        });
    }

 //门面
function fn_shopGrid() {
    $('#shoplistGrid').datagrid({
        fit: true,
        fitColumns: true,
        rownumbers: true,
        singleSelect: true,
        pagination: true, 
        pageSize:'20',
        toolbar: [{
            id: 'btnsearch-detail',
            text: '查找',
            iconCls: 'icon-search',
            handler: function() {
                $('#dialog-shopsearch').dialog({ title: '查找门面' });
                 $("#shopsearchform").form('clear');
                $('#dialog-shopsearch').dialog('open');               
            }
        },{
            id: 'btnreload-detail',
            text: '刷新',
            iconCls: 'icon-reload',
            handler: function() {
               $('#shoplistGrid').datagrid('options').url = '../Controller.aspx?controller=SysInitController&method=GetShopList&shopname=';
               $('#shoplistGrid').datagrid('reload');
            }
        },{
            id: 'btnadd-detail',
            text: '添加',
            iconCls: 'icon-add',
            handler: function() {
                $('#dialog-addshop').dialog({ title: '添加门面' });
                $("#shopaddform").form('clear');
                $("#shopusearea").val(0);
                
                $('#dialog-addshop').dialog('open');               
            }
        }, {
            id: 'btnedit-detail',
            text: '修改',
            iconCls: 'icon-edit',
            handler: function() {
                 var selected = $('#shoplistGrid').datagrid('getSelected');
                if(selected!=null)
                {
                  $('#dialog-addshop').dialog({ title: '修改门面信息' });
                  $("#shopaddform").form("load", selected);
                  var shopid=selected.ShopId;
                  $("#formshopid").val(shopid);
                  $('#shopsroomGrid').datagrid('options').url = '../Controller.aspx?controller=RoomController&method=GetShopsRoom&ShopId='+shopid;
                  $('#shopsroomGrid').datagrid('reload'); 
                  $('#dialog-addshop').dialog('open');
                }
            }
        }, {
            id: 'btndel-detail',
            text: '删除',
            iconCls: 'icon-no',
            handler: function() {
                 var SelectedNode = $('#shoplistGrid').datagrid('getSelected');
                 if (SelectedNode!=null) {
                    if (!confirm("确定要删除吗？"))
                        return false;
                    var data = { ShopId: SelectedNode.ShopId };
                    requestAjax("../Controller.aspx?controller=RoomController&method=DelShop", data, function(retobject) {
                        $('#shoplistGrid').datagrid('reload');
                    });
                } 
            }
     }]
        });
    }
    
//新增、修改管理区域弹出框 
 function fn_dialog_area() {  
    $('#dialog-area').dialog({
        modal: true,
        inline: true,
        buttons: [{
            text: '确定',
            iconCls: 'icon-ok',
            handler: function() {              
                FormSubmit($('#areaform'), '../Controller.aspx?controller=RoomController&method=AddArea', function() {
                     $('#dialog-area').dialog('close');
                     $('#areaGrid').datagrid('reload');
                });
            }
         }, {
            text: '取消',
            iconCls: 'icon-cancel',
            handler: function() {
                $('#dialog-area').dialog('close');
            }
        }]
     });
     $('#dialog-area').dialog('close');
    }
    
//新增、修改楼宇弹出框   
function fn_dialog_building() {  
    $('#dialog-building').dialog({
        modal: true,
        inline: true,
        buttons: [{
            text: '确定',
            iconCls: 'icon-ok',
            handler: function() {
                FormSubmit($('#buildingform'), '../Controller.aspx?controller=RoomController&method=AddBuilding', function() {
                     $('#dialog-building').dialog('close');                
                      var SelectedNode = $('#areaGrid').datagrid('getSelected');
                     if (SelectedNode) {
                       var s=SelectedNode.AreaCode;
                       $('#buildingGrid').datagrid('options').url = '../Controller.aspx?controller=RoomController&method=GetBuildingList&AreaCode=' + s;
                       $('#buildingGrid').datagrid('reload');
                     }                  
                });
            }
         }, {
            text: '取消',
            iconCls: 'icon-cancel',
            handler: function() {
                $('#dialog-building').dialog('close');
            }
        }]
     });
     $('#dialog-building').dialog('close');
    }
 
//新增、修改房间弹出框 
function fn_dialog_room() {
    $('#dialog-room').dialog({
        modal: true,
        inline: true,
        buttons: [{
            text: '确定',
            iconCls: 'icon-ok',
            handler: function() {
               var val = $('#pBuilding').combobox('getValue');
               var floorNum=$('#floorNum').val();
               var construcArea=$('#ConstrucArea').val();//建筑面积
               var useArea=$('#UseArea').val();//使用面积
			    if(floorMax<floorNum)
			    {
			       alert("该数值大于本栋楼最大楼层数,请重新输入！");
			       return;
			    }
                if(construcArea<useArea)
			    {
			       alert("建筑面积小于使用面积,请重新输入！");
			       return;
			    }
                FormSubmit($('#roomform'), '../Controller.aspx?controller=RoomController&method=AddRoom&BuildingCode='+val, function() {
                     $('#dialog-room').dialog('close');
                     $('#roomGrid').datagrid('reload');
                });
            }
           }, {
            text: '取消',
            iconCls: 'icon-cancel',
            handler: function() {
                $('#dialog-room').dialog('close');
            }
        }]
     });
     $('#dialog-room').dialog('close');
    }
    
    //查找房间弹出框 
function fn_dialog_searchroom() {
    $('#dialog-search').dialog({
        modal: true,
        inline: true,
        buttons: [{
            text: '确定',
            iconCls: 'icon-search',
            handler: function() {
               var searchArea = $('#searchArea').combobox('getValue');
               var searchBuilding = $('#searchBuilding').combobox('getValue');
               var roomNumber = $('#RoomNumber').val();
               var useType = $('#useType').combobox('getValue');
                  $('#roomGrid').datagrid('options').queryParams={AreaCode:searchArea,BuildingCode:searchBuilding,UseType:useType,RoomNumber:roomNumber};
                  $('#roomGrid').datagrid('options').url = '../Controller.aspx?controller=RoomController&method=GetRoomList';
                  $('#dialog-search').dialog('close'); 
                  $('#roomGrid').datagrid('reload');
            }
         }, {
            text: '取消',
            iconCls: 'icon-cancel',
            handler: function() {
                $('#dialog-search').dialog('close');
            }
        }]
     });
     $('#dialog-search').dialog('close');
    }
    
//查找门面弹出框 
function fn_dialog_searchshop() {
    $('#dialog-shopsearch').dialog({
        modal: true,
        inline: true,
        buttons: [{
            text: '确定',
            iconCls: 'icon-search',
            handler: function() {
             var shopname = $('#txtshop').val();
                $('#shoplistGrid').datagrid('options').url = '../Controller.aspx?controller=SysInitController&method=GetShopList&shopname=' + shopname;
                $('#shoplistGrid').datagrid('reload');
                 $('#dialog-shopsearch').dialog('close');
            }
         }, {
            text: '取消',
            iconCls: 'icon-cancel',
            handler: function() {
                $('#dialog-shopsearch').dialog('close');
            }
        }]
     });
     $('#dialog-shopsearch').dialog('close');
    }
   
   
           
//新增门面弹出框   
function fn_dialog_addshop() {  
    $('#dialog-addshop').dialog({
        modal: true,
        inline: true,
        buttons: [{
            text: '保存',
            iconCls: 'icon-ok',
            handler: function() {
                    var rows=$("#shopsroomGrid").datagrid("getData");
                     if(rows.total==0)
                     {
                       alert("请至少选择一个房间^-^");
                       return false;
                      }
                      
                   var shopname= $('#fromshopname').val();
                   var usearea= $('#shopusearea').val();
                   var memo= $('#fromshopmemo').val();
                   var shopid= $('#formshopid').val();
                   var data ={data: $.toJSON(rows.rows),ShopId:shopid,ShopName:shopname,UseArea:usearea,Memo:memo};

                    requestAjax("../Controller.aspx?controller=RoomController&method=AddShop", data, function(retobject) {
                        $.messager.progress('close');
                        $('#roomGrid').datagrid('reload');
                    });
            }
         },{
            text: '取消',
            iconCls: 'icon-cancel',
            handler: function() {
                $('#dialog-addshop').dialog('close');
            }
        }]
     });
     $('#dialog-addshop').dialog('close');
    }
    
      //门面类型房间   
 function fn_ownerRoomGrid() {
    $('#ownerRoomGrid').datagrid({
        onDblClickRow: function(rowIndex, rowData) {
              var rows=$("#shopsroomGrid").datagrid("getData");
              for(var i=0;i<rows.total;i++)
              {
                 if(rowData.RoomId==rows.rows[i].RoomId)
                 {
                   alert("请不要重复添加同一房间^-^");
                   return;
                  }
              }
                  $("#shopsroomGrid").datagrid("appendRow", rowData);
                   var usearea=parseInt($('#shopusearea').val());
                  $('#shopusearea').val(usearea+parseInt(rowData.UseArea));   
            }
        });
    }  
    
  //查找门面
  function fn_searchshop()
  {
      var searchArea = $('#searchshopArea').combobox('getValue');
      var searchBuilding = $('#searchshopBuilding').combobox('getValue');
      var useType =3;
      var searchRoom = $('#searchshopId').val();
      $('#ownerRoomGrid').datagrid('options').queryParams={AreaCode:searchArea,BuildingCode:searchBuilding,UseType:useType,RoomNumber:searchRoom};
      $('#ownerRoomGrid').datagrid('options').url = '../Controller.aspx?controller=RoomController&method=GetRoomList';
      $('#ownerRoomGrid').datagrid('reload'); 
  }
  
  //删除门面绑定的房间
  function fn_delshoproomfield()
  {
       var node = $('#shopsroomGrid').datagrid('getSelected');
	   if(node==null)
        {
          alert('请您选择要删除的记录！');
          return;
        }
        if (!confirm("确定要删除吗？"))
          return;         
        var index=$('#shopsroomGrid').datagrid('getRowIndex',node)
        $('#shopsroomGrid').datagrid('deleteRow',index);   
        var usearea= parseInt($('#shopusearea').val());
        $('#shopusearea').val(usearea-parseInt(node.UseArea));
  }	
  
  //重置门面类型房间查询条件
  function fn_reset()
 {
      $('#searchshopArea').combobox('clear');
      $('#searchshopBuilding').combobox('clear');
      $('#useType').combobox('clear');
      $('#searchshopId').val("");
 }

  function fn_dialog_batchadd()
  {
        $('#dialog-batchadd').dialog({
            modal: true,
            inline: true
         });
         $('#dialog-batchadd').dialog('close');
  }
  
     
    function fn_batchdblclick()
    {
        var lastIndex;
       	lastIndex = $('#batchroomgrid').datagrid('getRows').length-1;
	    $('#batchroomgrid').datagrid({
	       onBeforeLoad:function(){
					$(this).datagrid('rejectChanges');
				},
			onClickRow:function(rowIndex){
				if (lastIndex != rowIndex){
					$('#batchroomgrid').datagrid('endEdit', lastIndex);
					$('#batchroomgrid').datagrid('beginEdit', rowIndex);
				}
				lastIndex = rowIndex;
			}
	    })
    }
   function batchroomadd_add()
   {
      	$('#batchroomgrid').datagrid('acceptChanges');
      	var rows=$('#batchroomgrid').datagrid('getData');
      	if(rows.total==0)
            return false;
        var floorNum=$('#FloorNum').val();
	    if(floorMax<floorNum)
	    {
	       alert("该数值大于本栋楼最大楼层数,请重新输入！");
	       return;
	    }
       if (!confirm("确定要批量生成吗？"))
            return false;  
        var BuildingCode = $('#batchaddbuilding').combobox('getValue');
        var AreaCode = $('#batchaddarea').combobox('getValue');
        var FloorNum= $('#FloorNum').val();
        var Rule= $('#Rule').val();
        var data ={data: $.toJSON(rows.rows),BuildingCode:BuildingCode,AreaCode:AreaCode,FloorNum:FloorNum,Rule:Rule};
        $.messager.progress({
            title: '批量生成房间',
            msg: '正在操作中请等待...'
        });
        requestAjax("../Controller.aspx?controller=RoomController&method=BatchAddRoom", data, function(retobject) {
            $.messager.progress('close');
            $('#roomGrid').datagrid('reload');
        }, function() { $.messager.progress('close');});
   }
   
    function batchroomadd_del()
   {
        var row = $('#batchroomgrid').datagrid('getSelected');
	    if (row){
		    var index = $('#batchroomgrid').datagrid('getRowIndex', row);
		    $('#batchroomgrid').datagrid('deleteRow', index);
	    }
   }
   
    function batchroomadd_cancel()
   {
      $('#dialog-batchadd').dialog('close');
   }
   
    function loadData(){
           var buildingCode="";
           var unit="";
          
			$('#pArea').combobox({
				onSelect: function(record) {
				  var s=record.AreaCode;
                   $('#pBuilding').combobox('options').url = '../Controller.aspx?controller=RoomController&method=GetBuildingFieldList&AreaCode=' + s;
                   $('#pBuilding').combobox('reload');
               }
			});
			$('#pBuilding').combobox({
				onSelect: function(record) {
				   buildingCode=record.BuildingCode;
				   floorMax=record.FloorNum;
//				   $('#pRoomNumber').val(buildingCode);
                   $('#punit').combobox('options').url = '../Controller.aspx?controller=RoomController&method=GetUnitComArray&BuildingCode=' + buildingCode;
                   $('#punit').combobox('reload');
               }
			});
//			$('#punit').combobox({
//				 onSelect: function(record) {
//				   unit=record.Code;
//				   $('#pRoomNumber').val(buildingCode+unit);
//               }
//			});
			$('#floorNum').numberspinner({
				onSpinUp: function() {
				var floorNum=$('#floorNum').val();
			    if(floorMax<floorNum)
			    {
			       alert("该数值大于本栋楼最大楼层数,请重新输入");
			        return;
			    }
               }
//               onSpinDown: function() {
//				 var floorNum=$('#floorNum').val();
//			     $('#pRoomNumber').val(buildingCode+unit+floorNum);
//               }

			});
			$('#searchshopArea').combobox({
				onSelect: function(record) {
				  var s=record.AreaCode;
                   $('#searchshopBuilding').combobox('options').url = '../Controller.aspx?controller=RoomController&method=GetBuildingFieldList&AreaCode=' + s;
                   $('#searchshopBuilding').combobox('reload');
               }
			});
			$('#pArea').combobox({
				onSelect: function(record) {
				  var s=record.AreaCode;
                   $('#pBuilding').combobox('options').url = '../Controller.aspx?controller=RoomController&method=GetBuildingFieldList&AreaCode=' + s;
                   $('#pBuilding').combobox('reload');
               }
			});
			$('#batchaddarea').combobox({
				onSelect: function(record) {
				  var s=record.AreaCode;
                   $('#batchaddbuilding').combobox('options').url = '../Controller.aspx?controller=RoomController&method=GetBuildingFieldList&AreaCode=' + s;
                   $('#batchaddbuilding').combobox('reload');
               }
			});
			$('#batchaddbuilding').combobox({
				onSelect: function(record) {
				  var buildingcode=record.BuildingCode;
				  floorMax=record.FloorNum;
				  var data = { BuildingCode:buildingcode };
                    requestAjax("../Controller.aspx?controller=RoomController&method=GetCellArray", data, function(retobject) {
					 $('#batchroomgrid').datagrid('loadData',retobject);
					 $('#FloorNum').val(retobject.rows[0].FloorNum);
                    });
				  
                 
               }
			});
		}

function areaCodeFormatter(value){
    var statdata=$('#areaGrid').datagrid('getData').rows;
	for(var i=0; i<statdata.length; i++){
		if (statdata[i].AreaCode == value) return statdata[i].AreaName;
	}
	return value;
}

function batchroomadd_load()
{
    $('#batchaddarea').combobox('clear');
    $('#batchaddbuilding').combobox('clear');
     $('#batchroomgrid').datagrid('reload');   
}
		
    //3.初始化入口
    $(function() {
        fn_areaGrid();
        fn_buildingGrid();
        fn_roomGrid();
        fn_dialog_area();
        fn_dialog_building();
        fn_dialog_searchroom();
        fn_dialog_room();
        fn_dialog_batchadd();
        fn_batchdblclick();
        loadData();
        fn_shopGrid();
        fn_dialog_searchshop();
        fn_dialog_addshop();
        fn_ownerRoomGrid();
    });
    
  